package cn.seqdata.tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public abstract class TreeNode {
	private boolean container;
	private Boolean expanded;
	private Boolean checked;
	private String iconCls;
	private List<TreeNode> children;
	/** 输出到前台的默认扩展属性 */
	public final Map<String, Object> exts = new HashMap<>();

	public TreeNode(boolean container) {
		this.container = container;
	}

	public TreeNode(boolean container, boolean expanded) {
		this(container);
		this.expanded = expanded;
	}

	public abstract String getId();

	public abstract String getText();

	public abstract String getClz();

	public boolean isLeaf() {
		return !container;
	}

	public void setLeaf(boolean leaf) {
		this.container = !leaf;
		this.expanded = false;
	}

	@JsonInclude(Include.NON_NULL)
	public Boolean getExpanded() {
		return expanded;
	}

	public void setExpanded(Boolean expanded) {
		this.expanded = expanded;
	}

	@JsonInclude(Include.NON_NULL)
	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	@JsonInclude(Include.NON_NULL)
	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	@JsonInclude(Include.NON_NULL)
	public Collection<? extends TreeNode> getData() {
		return container ? children : null;
	}

// @JsonInclude(Include.NON_NULL)
// public List<TreeNode> getChildren() {
// return container ? children : null;
// }

	public void addChild(TreeNode child) {
		children().add(child);
	}

	public void addChildren(Collection<? extends TreeNode> children) {
		children().addAll(children);
	}

	public List<TreeNode> children() {
		if (null == children)
			children = new ArrayList<>();
		return children;
	}
}
